// pages/my/getInspiration/index.js
const {
  myApi
} = require('../../../api/index')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IMGBASEURL: app.globalData.IMGBASEURL,
    isall: false,
    userPhone: "",
    userCode: "",
    verification: 60,
    isVerification: true,
    isMengcheng: false,
    typeUser: 1,
    inspiration: 0,
    btn: true,
    key: "",
    num: 0
  },

  // 输入电话
  inputChange(e) {
    this.setData({
      userPhone: e.detail.value
    })
  },
  // 输入验证码
  userCodeChange(e) {
    this.setData({
      userCode: e.detail.value
    })
  },
  // 点击获取验证码
  gain() {
    if (this.data.isVerification) {
      this.setData({
        isVerification: false
      })
      let time = setInterval(() => {
        this.setData({
          verification: this.data.verification - 1
        })
        if (this.data.verification == 0) {
          clearInterval(time)
          this.setData({
            isVerification: true,
            verification: 60
          })
        }
      }, 1000);
    }

  },

  // 验证
  rules() {
    if (this.data.userPhone.length < 11) {
      wx.showToast({
        title: '请输入正确电话号码',
        icon: "none"
      })
      return false
    }
    return true
  },
  // 点击领取红包
  getPacket() {
    let thet = this
    console.log("options.key", thet.data.key);
    console.log("存的", wx.getStorageSync('inspirationKey'));
    if (this.rules()) {
      wx.showLoading()
      myApi.isuserasync({
        PhoneNumber: thet.data.userPhone
      }).then((item) => {
        if (item.Result) {
          myApi.communitywelfare({
            GetWelfare: {
              Key: wx.getStorageSync('inspirationKey'),
              PhoneNumber: thet.data.userPhone
            }
          }).then((item) => {
            console.log('我的参数，', {
              GetWelfare: {
                Key: wx.getStorageSync('inspirationKey'),
                PhoneNumber: thet.data.userPhone
              }
            });
            console.log("你返回的", item);
            if (item.StatusCode != 200) {
              wx.showToast({
                title: "电话号码错误或已领取过",
                icon: 'none'
              })
            } else {
              this.setData({
                inspiration: item.Result[0].Count,
                btn: false,
                isMengcheng: true
              })
            }
          })
        } else {
          wx.navigateTo({
            url: '/pages/login/index?backPageUrl=pages/my/getInspiration/index'
          })
        }
        wx.hideLoading()
      })
    }

  },
  //  关闭弹窗
  cloeMengcheng() {
    this.setData({
      isMengcheng: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.key) {
      wx.showLoading()
      wx.setStorageSync('inspirationKey', options.key)
      myApi.welfareactivitydetail({
        Key: options.key,
        ActivePort: 3
      }).then(e => {
        this.setData({
          num: e.Result.AwardList[0]?.Count || 0
        })
        wx.hideLoading()
      }).catch(e => {
        wx.hideLoading()
      })
      this.setData({
        key: options.key
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})